package StudentSystem;
/**
 * Test类是实现所有功能的主函数入口
 */

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Test {
    private static final int ADD_STUDENT = 1;
    private static final int REMOVE_STUDENT = 2;
    private static final int CHANGE_STUDENT = 3;
    private static final int SEARCH_STUDENT = 4;
    private static final int RANK_STUDENT = 5;
    private static final int ANALYSE_STUDENT = 6;
    private static final int PRINT_STUDENT = 7;
    private static final int EXIT = 8;
    private static final int DEFAULT_MAX_CLASSROOM_NUMBER = 10;

    public static void main(String[] args) {
        System.out.println("-------------欢迎来到学生成绩管理系统----------------");
        ArrayList<Student> list = new ArrayList<>();
        initData(list);
        init(list);
    }

    public static void initData(ArrayList<Student> list) {
        for (int i = 1; i <= DEFAULT_MAX_CLASSROOM_NUMBER; i++) {
            String filename = "Data/计科" + i + "班.csv";
            try {
                List<String> lines = Files.readAllLines(Paths.get(filename));
                for (String line : lines) {
                    String[] data = line.split(",");
                    Student temp = new Student();
                    temp.setId(data[0]);
                    temp.setName(data[1]);
                    temp.setClassroom(data[2]);
                    temp.setMath(Double.parseDouble(data[3]));
                    temp.setEnglish(Double.parseDouble(data[4]));
                    temp.setComputer(Double.parseDouble(data[5]));
                    temp.setAverage(Double.parseDouble(data[6]));
                    list.add(temp);
                }
            } catch (IOException e) {
                break;
            }
        }
    }

    public static void init(ArrayList<Student> list) {
        System.out.println("1：添加学生");
        System.out.println("2：删除学生");
        System.out.println("3：修改学生");
        System.out.println("4：查询学生");
        System.out.println("5：学生成绩排名");
        System.out.println("6：班级成绩分析");
        System.out.println("7：输出学生信息表");
        System.out.println("8：退出");
        System.out.println("请输入您的选择:");
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        switch (n) {
            case ADD_STUDENT:
                add(list);
            case REMOVE_STUDENT:
                remove(list);
            case CHANGE_STUDENT:
                change(list);
            case SEARCH_STUDENT:
                search(list);
            case RANK_STUDENT:
                rank(list);
            case ANALYSE_STUDENT:
                analyse(list);
            case PRINT_STUDENT:
                printStudent(list);
            case EXIT:
                System.exit(0);
        }
    }

    public static void add(ArrayList<Student> list) {
        Scanner sc = new Scanner(System.in);
        Student s = new Student();
        System.out.println("请输入学生学号");
        String id = sc.next();
        int index = getIndex(list, id);
        if (index != -1) {
            System.out.println("输入学号重复，请重新输入");
            add(list);
        }
        System.out.println("请输入学生姓名");
        String name = sc.next();
        System.out.println("请输入学生班级");
        String classroom = sc.next();
        System.out.println("请输入学生数学成绩");
        double Math = sc.nextInt();
        System.out.println("请输入学生英语成绩");
        double English = sc.nextInt();
        System.out.println("请输入学生计算机成绩");
        double Computer = sc.nextInt();
        double average = (Math + English + Computer) / 3.0;
        String sAverage = String.format("%.2f", average);
        s.setId(id);
        s.setName(name);
        s.setClassroom(classroom);
        s.setMath(Math);
        s.setEnglish(English);
        s.setComputer(Computer);
        s.setAverage(Double.parseDouble(sAverage));
        list.add(s);
        for (int i = 1; i <= DEFAULT_MAX_CLASSROOM_NUMBER; i++) {
            if (classroom.equals("0" + i)) {
                String filename = "Data/计科" + i + "班.csv";
                String data = id + "," + name + "," + classroom + "," + Math + "," + English + "," + Computer + "," + sAverage;
                try (BufferedWriter writer = new BufferedWriter(new FileWriter(filename, true))) {
                    writer.write(data);
                    writer.newLine();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        System.out.println("添加成功!");
        System.out.println("是否继续添加(1为是2为否)");
        int n = sc.nextInt();
        if (n == 1) {
            add(list);
        } else {
            init(list);
        }
    }

    public static void remove(ArrayList<Student> list) {
        System.out.println("请选择需要删除的学生(学号)");
        Scanner sc = new Scanner(System.in);
        String id = sc.next();
        int index = getIndex(list, id);
        if (index == -1) {
            System.out.println("输入学号有误，请重新输入");
            remove(list);
        } else {
            Student s = list.get(index);
            for (int i = 1; i <= DEFAULT_MAX_CLASSROOM_NUMBER; i++) {
                if (s.getClassroom().equals("0" + i)) {
                    String filename = "Data/计科" + i + "班.csv";
                    try {
                        List<String> lines = new ArrayList<>();
                        try (BufferedReader reader = new BufferedReader(new FileReader(filename))) {
                            String currentLine;
                            while ((currentLine = reader.readLine()) != null) {
                                lines.add(currentLine);
                            }
                        }
                        lines.removeIf(line -> line.contains(id));
                        try (BufferedWriter writer = new BufferedWriter(new FileWriter(filename))) {
                            for (String line : lines) {
                                writer.write(line);
                                writer.newLine();
                            }
                        }
                        System.out.println("特定行已被删除。");
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    //当找到对应班级的对应学生时 不需要继续找 应立刻停止
                    break;
                }
            }
            list.remove(index);
        }
        System.out.println("删除成功!");
        System.out.println("是否继续删除(1为是2为否)");
        int n = sc.nextInt();
        if (n == 1) {
            remove(list);
        } else {
            init(list);
        }
    }

    public static void changeFile(String changeInfo, Student s) {
        for (int i = 1; i <= DEFAULT_MAX_CLASSROOM_NUMBER; i++) {
            if (s.getClassroom().equals("0" + i)) {
                String filename = "Data/计科" + i + "班.csv";
                try {
                    // 读取文件的所有行到一个列表中
                    List<String> lines = Files.readAllLines(Paths.get(filename));
                    // 遍历列表，找到匹配的行并修改
                    for (int j = 0; j < lines.size(); j++) {
                        if (lines.get(j).contains(s.getId())) {
                            lines.set(j, changeInfo); // 修改找到的行
                            break; // 如果只有一个匹配的行，找到后可以停止循环
                        }
                    }
                    // 将更新后的列表写回到文件中
                    Files.write(Paths.get(filename), lines);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                //当找到对应班级的对应学生时 不需要继续找 应立刻停止
                break;
            }
        }
    }

    public static void change(ArrayList<Student> list) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请选择需要修改的学生学号");
        String id = sc.next();
        int index = getIndex(list, id);
        if (index == -1) {
            System.out.println("输入学号有误，请重新输入");
            remove(list);
        }
        System.out.println("请选择需要修改的内容1姓名2班级3数学成绩4英语成绩5计算机成绩");
        int n = sc.nextInt();
        Student s = list.get(index);
        double average;
        switch (n) {
            case 1:
                System.out.println("请输入修改后的内容");
                String name = sc.next();
                String changeName = s.getId() + "," + name + "," + s.getClassroom() + "," + s.getMath() + "," +
                        s.getEnglish() + "," + s.getComputer() + "," + s.getAverage();
                changeFile(changeName, s);
                s.setName(name);
                list.set(index, s);
                System.out.println("修改成功!");
                init(list);
            case 2:
                System.out.println("请输入修改后的内容");
                String classroom = sc.next();
                String changeClassroom = s.getId() + "," + s.getName() + "," + classroom + "," + s.getMath()
                        + "," + s.getEnglish() + "," + s.getComputer() + "," + s.getAverage();
                changeFile(changeClassroom, s);
                s.setClassroom(classroom);
                list.set(index, s);
                System.out.println("修改成功!");
                init(list);
            case 3:
                System.out.println("请输入修改后的内容");
                int math = sc.nextInt();
                average = (math + s.getEnglish() + s.getComputer()) / 3.0;
                double mAverage = Double.parseDouble(String.format("%.2f", average));
                String changeMath = s.getId() + "," + s.getName() + "," + s.getClassroom() + "," + math + "," +
                        s.getEnglish() + "," + s.getComputer() + "," + mAverage;
                changeFile(changeMath, s);
                s.setMath(math);
                s.setAverage(mAverage);
                list.set(index, s);
                System.out.println("修改成功!");
                init(list);
            case 4:
                System.out.println("请输入修改后的内容");
                int English = sc.nextInt();
                average = (s.getMath() + English + s.getComputer()) / 3.0;
                double eAverage = Double.parseDouble(String.format("%.2f", average));
                String changeEnglish = s.getId() + "," + s.getName() + "," + s.getClassroom() + ","
                        + s.getMath() + "," + English + "," + s.getComputer() + "," + eAverage;
                changeFile(changeEnglish, s);
                s.setEnglish(English);
                s.setAverage(eAverage);
                list.set(index, s);
                System.out.println("修改成功!");
                init(list);
            case 5:
                System.out.println("请输入修改后的内容");
                int Computer = sc.nextInt();
                average = (s.getMath() + s.getEnglish() + Computer) / 3.0;
                double cAverage = Double.parseDouble(String.format("%.2f", average));
                String changeComputer = s.getId() + "," + s.getName() + "," + s.getClassroom() + ","
                        + s.getMath() + "," + s.getEnglish() + "," + s.getComputer() + "," + cAverage;
                changeFile(changeComputer, s);
                s.setComputer(Computer);
                s.setAverage(cAverage);
                list.set(index, s);
                System.out.println("修改成功!");
                init(list);
        }
    }

    public static void search(ArrayList<Student> list) {
        System.out.println("请输入查询学生的学号或姓名");
        Scanner sc = new Scanner(System.in);
        String message = sc.next();
        int index = -1;
        for (int i = 0; i < list.size(); i++) {
            Student s = list.get(i);
            if (message.equals(s.getId()) || message.equals(s.getName())) {
                index = i;
                break;
            }
        }
        if (index != -1) {
            Student s = list.get(index);
            System.out.println("学号" + "\t" + "姓名" + "\t" + "班级" + "\t" + "数学" + "\t" + "英语" + "\t" + "计算机" + "\t" + "平均分" + "\t");
            System.out.println(s.getId() + "\t" + s.getName() + "\t" + s.getClassroom() + "\t"
                    + s.getMath() + "\t" + s.getEnglish() + "\t" + s.getComputer() + "\t" + s.getAverage());
            System.out.println("查询成功!");
            init(list);
        } else {
            System.out.println("暂无此学生,请重新输入");
            search(list);
        }
    }

    public static void rank(ArrayList<Student> list) {
        ArrayList<Student> rankList = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            rankList.add(i, list.get(i));
        }
        System.out.println("请选择排序的方式1.平均成绩2.计算机成绩");
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        for (int j = 0; j < rankList.size() - 1; j++) {
            for (int i = 0; i < rankList.size() - 1 - j; i++) {
                Student s1 = rankList.get(i);
                Student s2 = rankList.get(i + 1);
                if (n == 1) {
                    if (s1.getAverage() > s2.getAverage()) {
                        rankList.set(i, s2);
                        rankList.set(i + 1, s1);
                    }
                } else if (n == 2) {
                    if (s1.getComputer() > s2.getComputer()) {
                        rankList.set(i, s2);
                        rankList.set(i + 1, s1);
                    }
                } else {
                    System.out.println("输入错误，请重新输入");
                    rank(list);
                }
            }
        }
        System.out.println("学号" + "\t" + "姓名" + "\t" + "班级" + "\t" + "数学" + "\t"
                + "英语" + "\t" + "计算机" + "\t" + "平均分" + "\t");
        for (int i = rankList.size() - 1; i >= 0; i--) {
            Student s = rankList.get(i);
            System.out.println(s.getId() + "\t" + s.getName() + "\t" + s.getClassroom() + "\t"
                    + s.getMath() + "\t" + s.getEnglish() + "\t" + s.getComputer() + "\t" + s.getAverage());
        }
        System.out.println("排名成功!");
        init(list);
    }

    public static void analyse(ArrayList<Student> list) {
        System.out.println("请选择查看的班级");
        Scanner sc = new Scanner(System.in);
        String classroom = sc.next();
        int CountStudent = 0;
        double SumMath = 0;
        double SumEnglish = 0;
        double SumComputer = 0;
        double MaxMath = 0;
        double MaxEnglish = 0;
        double MaxComputer = 0;
        double MinMath = 100;
        double MinEnglish = 100;
        double MinComputer = 100;
        int CountPassMath = 0;
        int CountPassEnglish = 0;
        int CountPassComputer = 0;
        for (int i = 0; i < list.size(); i++) {
            Student s = list.get(i);
            if (classroom.equals(s.getClassroom())) {
                SumMath += s.getMath();
                SumEnglish += s.getEnglish();
                SumComputer += s.getComputer();
                CountStudent++;
                if (s.getMath() > MaxMath) {
                    MaxMath = s.getMath();
                }
                if (s.getEnglish() > MaxEnglish) {
                    MaxEnglish = s.getEnglish();
                }
                if (s.getComputer() > MaxComputer) {
                    MaxComputer = s.getComputer();
                }
                if (s.getMath() < MinMath) {
                    MinMath = s.getMath();
                }
                if (s.getEnglish() < MinEnglish) {
                    MinEnglish = s.getEnglish();
                }
                if (s.getComputer() < MinComputer) {
                    MinComputer = s.getComputer();
                }
                if (s.getMath() >= 60) {
                    CountPassMath++;
                }
                if (s.getEnglish() >= 60) {
                    CountPassEnglish++;
                }
                if (s.getComputer() >= 60) {
                    CountPassComputer++;
                }
            }
        }
        if (CountStudent == 0) {
            System.out.println("班级输入错误，请重新输入");
            analyse(list);
        }
        double AveMath = Double.parseDouble(String.format("%.2f", SumMath / CountStudent));
        double AveEnglish = Double.parseDouble(String.format("%.2f", SumEnglish / CountStudent));
        double AveComputer = Double.parseDouble(String.format("%.2f", SumComputer / CountStudent));
        double PassMath = Double.parseDouble(String.format("%.4f", (CountPassMath * 1.0) / CountStudent)) * 100.0;
        double PassEnglish = Double.parseDouble(String.format("%.4f", (CountPassEnglish * 1.0) / CountStudent)) * 100.0;
        double PassComputer = Double.parseDouble(String.format("%.4f", (CountPassComputer * 1.0) / CountStudent)) * 100.0;
        System.out.println(classroom + "\t" + "平均成绩" + "\t" + "最高分" + "\t" + "最低分" + "\t" + "及格率");
        System.out.println("数学"+"\t" + AveMath + "\t\t" + MaxMath + "\t" + MinMath + "\t" + PassMath + "%");
        System.out.println("英语"+"\t" + AveEnglish + "\t\t" + MaxEnglish + "\t" + MinEnglish + "\t" + PassEnglish + "%");
        System.out.println("计算机"+"\t" + AveComputer + "\t\t" + MaxComputer + "\t" + MinComputer + "\t" + PassComputer + "%");
        System.out.println("分析成功!");
        init(list);
    }

    public static void printStudent(ArrayList<Student> list) {
        System.out.println("学号" + "\t" + "姓名" + "\t" + "班级" + "\t" + "数学" + "\t" + "英语" + "\t" + "计算机" + "\t" + "平均分" + "\t");
        for (int i = 0; i < list.size(); i++) {
            Student s = list.get(i);
            System.out.println(s.getId() + "\t" + s.getName() + "\t" + s.getClassroom() + "\t"
                    + s.getMath() + "\t" + s.getEnglish() + "\t" + s.getComputer() + "\t" + s.getAverage());
        }
        System.out.println("输出成功!");
        init(list);
    }

    public static int getIndex(ArrayList<Student> list, String id) {
        for (int i = 0; i < list.size(); i++) {
            Student s = list.get(i);
            if (id.equals(s.getId())) {
                return i;
            }
        }
        return -1;
    }
}